package com.ikunikun.controller.admin;

import com.ikunikun.dto.SetmealDTO;
import com.ikunikun.dto.SetmealPageQueryDTO;
import com.ikunikun.result.PageResult;
import com.ikunikun.result.Result;
import com.ikunikun.service.SetmealService;
import com.ikunikun.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

	@Resource
	private SetmealService setmealService;

	/**
	 * 新增套餐
	 * @param setmealDTO
	 * @return
	 */
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	@PostMapping
	@ApiOperation("新增套餐")
	public Result<String> saveWithDish(@RequestBody SetmealDTO setmealDTO){
		log.info("新增套餐：{}", setmealDTO);
		setmealService.saveWithDish(setmealDTO);
		return Result.success("新增套餐成功");
	}

	/**
	 * 套餐分页查询
	 * @param setmealPageQueryDTO
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperation("套餐分页查询")
	public Result page(SetmealPageQueryDTO setmealPageQueryDTO){
		log.info("套餐分页查询：{}", setmealPageQueryDTO);
		PageResult pageResult= setmealService.page(setmealPageQueryDTO);
		return Result.success(pageResult);
	}

	/**
	 * 套餐状态修改
	 * @param status
	 * @param id
	 * @return
	 */
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	@ApiOperation("套餐状态修改")
	@PostMapping("/status/{status}")
	public Result<String> startOrStop(@PathVariable Integer status, Long id){
		setmealService.startOrStop(status, id);
		return Result.success("套餐销售状态修改成功");
	}

	/**
	 * 根据套餐id获取套餐信息和套参与餐品关系
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@ApiOperation("根据套餐id获取套餐信息和套参与餐品关系")
	public Result<SetmealVO> getSetmealDishBySetmealId(@PathVariable Long id){
		SetmealVO setmealVo = setmealService.getSetmealAndSetmealDishBySetmealId(id);
		return Result.success(setmealVo);
	}

	/**
	 * 修改套餐信息
	 * @param setmealDTO
	 * @return
	 */
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	@PutMapping
	public Result<String> update(@RequestBody SetmealDTO setmealDTO){
		setmealService.update(setmealDTO);
		return Result.success("修改套餐成功");
	}

	/**
	 * 批量删除套餐
	 * @param ids
	 * @return
	 */
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	@DeleteMapping
	@ApiOperation("批量删除套餐")
	public Result<String> delete(@RequestParam List<Long> ids){
		setmealService.delete(ids);
		return Result.success("删除套餐成功");
	}
}
